package com.ch.mobilesha.base;

import android.app.Activity;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.ch.mobilesha.R;
import com.ch.mobilesha.common.CommonData;
import com.ch.mobilesha.tools.UtilTools;
import com.ch.mobilesha.tools.ViewTools;

public abstract class BaseActivity extends Activity implements OnClickListener {

	protected String BACK = "返回";

	public BaseActivity() {
		mApp = MobileShaApplication.getSelf();
	}

	public static final int RIGHT_BUTTON = 0;
	public static final int BG_BUTTON1 = 2;
	public static final int BG_BUTTON2 = 3;
	public static final int LEFT_BUTTON = 1;
	public RelativeLayout baseLayout;
	public Activity mContext;
	protected MobileShaApplication mApp;
	protected LayoutInflater mInflater;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		registerReceiver(mExitAppReceiver, new IntentFilter(CommonData.INTENT_ACTION_EXIT_APP));
		mContext = this;
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		initData(getIntent());
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		baseLayout = new RelativeLayout(this);
		baseLayout.setLayoutParams(layoutParams);
		CommonData.currentActivity = this;
	}

	protected void onResume() {
		super.onResume();
		CommonData.currentActivity = this;
	}

	protected void onPause() {
		super.onPause();
	}

	/**
	 * 退出app的时候发送一条广播，所有子类都将finish()
	 */
	private BroadcastReceiver mExitAppReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();
		}
	};

	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
		if (getParent() instanceof TabActivity) {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				mContext = getParent();
				ViewTools.showConfirm(mContext, "提示信息", "确认要退出吗？", "确定", "取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						mApp.exit();
						int id = android.os.Process.myPid();
						android.os.Process.killProcess(id);
					}
				}, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				return false;
			}
		}
		return super.onKeyDown(keyCode, event);
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	protected void onDestroy() {
		if (mContext == this) {
			unregisterReceiver(mExitAppReceiver);
		}
		super.onDestroy();

	}

	// 处理标题栏事件:
	protected abstract void handlerTitleBarEvent(int mode);

	protected abstract void initData(Intent intent);

	protected abstract void initView();

	public void setTitle(String left, String middle, String right) {
		baseLayout.addView(addTitle(this, left, middle, right));
	}

	public void setViews(int layoutId) {
		LayoutInflater iInflater = this.getLayoutInflater();
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.FILL_PARENT);
		this.addContentView(this.baseLayout, layoutParams);
		ViewGroup mainView = (ViewGroup) iInflater.inflate(layoutId, null);
		layoutParams.addRule(RelativeLayout.BELOW, R.id.titleBar);
		this.baseLayout.addView(mainView, layoutParams);
		initView();
	}

	/**
	 * 获取一个头部
	 * 
	 * @param context
	 * @param left
	 * @param middle
	 * @param right
	 * @return
	 */
	public View addTitle(Context context, String left, String middle, String right) {
		LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View titleView = mInflater.inflate(R.layout.titlebar, null);
		TextView leftView = (TextView) titleView.findViewById(R.id.titleLeft);
		TextView middleView = (TextView) titleView.findViewById(R.id.titleMiddle);
		TextView rightView = (TextView) titleView.findViewById(R.id.titleRight);
		if (!UtilTools.isNotNullOrEmpty(left)) {
			leftView.setVisibility(View.GONE);
		} else if (left!=null && left.endsWith("png")) {
			LayoutParams params = (LayoutParams) leftView.getLayoutParams();
			params.leftMargin = 100;
			leftView.setLayoutParams(params);
			if(mApp.getPictrueManager().getBitmapFromCache(left)!=null){
				leftView.setBackgroundDrawable(new BitmapDrawable(mApp.getPictrueManager().getBitmapFromCache(left)));
			}else{
				leftView.setBackgroundResource(R.drawable.a_image_loding);
			}
			leftView.setOnClickListener(this);
		} else {
			// leftView.setText(left);
			leftView.setOnClickListener(this);
		}
		if (!UtilTools.isNotNullOrEmpty(middle)) {
			middleView.setVisibility(View.GONE);
		} else {
			middleView.setOnClickListener(this);
			middleView.setText(middle);
		}
		if (!UtilTools.isNotNullOrEmpty(right)) {
			rightView.setVisibility(View.GONE);
		} else {
			rightView.setOnClickListener(this);
			if ("搜索".equals(right)) {
			} else {
				rightView.setText(right);
			}
		}
		return titleView;
	}

//	/**
//	 * 预处理progressbg
//	 */
//	protected void onPreExecuteProgress(View moreView) {
//		try {
//			ProgressBar progressbg = (ProgressBar) moreView.findViewById(R.id.pbLoading);
//			progressbg.setVisibility(View.VISIBLE);
//			progressbg.setEnabled(false);
//			TextView tvProgress = (TextView) moreView.findViewById(R.id.tvMore);
//			if (tvProgress != null)
//				tvProgress.setVisibility(View.GONE);
//			if (progressbg != null)
//				progressbg.setVisibility(View.VISIBLE);
//		} catch (Exception e) {
//		}
//
//	}
//
//	/**
//	 * 加载完处理progressbg
//	 */
//	protected void onPostExecuteProgress(View moreView) {
//		try {
//			ProgressBar progressbg = (ProgressBar) moreView.findViewById(R.id.pbLoading);
//			TextView tvProgress = (TextView) moreView.findViewById(R.id.tvMore);
//			progressbg.setVisibility(View.VISIBLE);
//			progressbg.setEnabled(true);
//			if (tvProgress != null)
//				tvProgress.setVisibility(View.VISIBLE);
//			if (progressbg != null)
//				progressbg.setVisibility(View.GONE);
//		} catch (Exception e) {
//		}
//
//	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.titleLeft:
			handlerTitleBarEvent(LEFT_BUTTON);
			break;
		case R.id.titleRight:
			handlerTitleBarEvent(RIGHT_BUTTON);
			break;
		case R.id.titleMiddle:
			handlerTitleBarEvent(BG_BUTTON1);
			break;
		}
	}
}
